package cn.iocoder.yudao.framework.business.eligibility.dao;

import cn.iocoder.yudao.framework.business.audit.enums.AuditStatusEnum;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.eligibility.pojo.po.EligibilityApplyEntity;
import jakarta.annotation.Nullable;

/**
 * 用户身份资格申请 Mapper
 *
 * @author 山野羡民
 */
//@org.apache.ibatis.annotations.Mapper
public interface EligibilityApplyMapper<DO extends EligibilityApplyEntity> extends CoreMapper<DO> {

    default DO selectByUserIdAndUserType(Long userId, @Nullable Integer userType) {
        if (userId == null || userId <= 0) {
            return null;
        }
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (userType != null) { // 传递 userType 为 null 表示数据表里不存在 user_type 字段，默认为 MEMBER 类型
            wrapper.eq("user_type", userType);
        }
        wrapper.limitN(1);
        return selectOne(wrapper);
    }

    default int selectAuditPassCount(Long userId, @Nullable Integer userType) {
        CoreQueryWrapper<DO> wrapper = new CoreQueryWrapper<>();
        wrapper.eq("user_id", userId);
        if (userType != null) { // 传递 userType 为 null 表示数据表里不存在 user_type 字段，默认为 MEMBER 类型
            wrapper.eq("user_type", userType);
        }
        wrapper.eq("audit_status", AuditStatusEnum.PASS.getValue());
        Long count = selectCount(wrapper);
        if (count == null) {
            return 0;
        }
        return count.intValue();
    }

}